home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 27 / CU Amiga Magazine's Super CD-ROM 27 (1998)(EMAP Images)(GB)[!][issue 1998-10].iso / CUCD / Utilities / mbackup / mbackup.c < prev    next >
C/C++ Source or Header  |  1998-08-02  |  8KB  |  272 lines

  1. /****h* MiniBackup/mbackup.c [1.20] *
  2. *
  3. *  NAME
  4. *    mbackup.c
  5. *  COPYRIGHT
  6. *    $VER: mbackup.c 1.20 (02.08.98) © by Stefan Kost 1997-1998
  7. *  FUNCTION
  8. *    backups files according to a taskfile using lha
  9. *  AUTHOR
  10. *    Stefan Kost
  11. *  CREATION DATE
  12. *    01.Aug.1998
  13. *  MODIFICATION HISTORY
  14. *    02.Aug.1998    V 1.20    extended args
  15. *    05.Jan.1998    V 1.10    stack-bugs fixed
  16. *    28.May.1997    V 1.00    first release
  17. *    19.May.1997    V 0.00    initial version
  18. *  NOTES
  19. *    requires lha to be in dos path
  20. *******
  21. */
  22.  
  23. //-- includes -----------------------------------------------------------------
  24.  
  25. //-- Ansi
  26.  
  27. #include <dos.h>
  28. #include <math.h>
  29. #include <mffp.h>                /* die entsprechenden Matheroutinen includen */
  30. #include <mieeedoub.h>
  31. #include <m68881.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include <time.h>
  36.  
  37. //-- AmigaOS
  38.  
  39. #include <exec/types.h>
  40. #include <cmacros.h>
  41. #include <dos/dos.h>
  42. #include <dos/dostags.h>
  43. #include <exec/exec.h>
  44.  
  45. #include <proto/dos.h>            /* Prototypes für Libraryfunctions */
  46. #include <proto/exec.h>
  47.  
  48. //-- Local
  49.  
  50. #include "project.h"
  51.  
  52. //-- DebugTools
  53.  
  54. #define DEBUG_SRCPATH "sc:source/tagtools/puttag/"
  55. #include <debug/dt_decl.h>
  56.  
  57. //-- prototypes ---------------------------------------------------------------
  58.  
  59. char *_stroserror(int errornum);
  60. void Error(STRPTR fmt,...);
  61. ULONG seekdir(FILE *wf,char *path,char *pat,UBYTE mode,ULONG nr);
  62.  
  63. //-- globals ------------------------------------------------------------------
  64.  
  65. UBYTE *VersTag="\0$VER:"PRJ_VERS;
  66.  
  67. #define ARG_TEMPLATE        "PATH/A,TASKFILE/A,COM=COMPLETE/S,INC=INCREMENTAL/S"
  68. #define ARG_COUNT            4
  69. #define ARGID_PATH            0
  70. #define ARGID_TASKFILE        1
  71. #define ARGID_COMPLETE        2
  72. #define ARGID_INCREMENTAL    3
  73.  
  74. #define MODE_NONE            0
  75. #define MODE_COMPLETE        1
  76. #define MODE_INCREMENTAL    2
  77.  
  78. //-- externals ----------------------------------------------------------------
  79.  
  80. #pragma msg 72 ignore
  81. extern long __near __stack=50000L;        /* reseve more stackspace; used by startup-code */
  82. #pragma msg 72 warning
  83.  
  84. //-- definitions --------------------------------------------------------------
  85.  
  86. char *stroserror(int errornum)
  87. {
  88.     static char *deferr="unknown",*err;
  89.     long i=0;
  90.  
  91.     err=deferr;
  92.     while(i!=-1 && __os_errlist[i].errnum)
  93.     {
  94.         if(__os_errlist[i].errnum==errornum)
  95.         {
  96.             err=__os_errlist[i].msg;i=-1;
  97.         }
  98.         else i++;
  99.     }
  100.     return(err);
  101. }
  102.  
  103. void Error(STRPTR fmt,...)
  104. {
  105.     va_list arglist;
  106.     char str[256];
  107.     int C_Error=errno;
  108.     long IOError=IoErr();
  109.     int OSError=_OSERR;
  110.  
  111.     va_start(arglist,fmt);
  112.     printf(PRJ_NAME" : ");
  113.     vprintf(fmt,arglist);
  114.     if(C_Error) printf("\terrno : %s\n",strerror(C_Error));
  115.     if(OSError) printf("\toserr : %s\n",stroserror(OSError));
  116.     if(IOError) { Fault(IOError,"\tioerr ",str,255);printf("%s\n",str); }
  117.     va_end(arglist);
  118. }
  119.  
  120. ULONG seekdir(FILE *wf,char *path,char *pat,UBYTE mode,ULONG nr)
  121. {
  122.     struct FileLock *dirlock;
  123.     struct FileInfoBlock InfoBlock;
  124.     char newpath[256];
  125.  
  126.     MSG2("files=%5ld scanning %s",nr,path);
  127.     if(dirlock=(struct FileLock *)Lock(path,SHARED_LOCK))
  128.     {
  129.         if(Examine((BPTR)dirlock,&InfoBlock))
  130.         {
  131.             if(InfoBlock.fib_DirEntryType>0 && InfoBlock.fib_DirEntryType!=3)    // it's a directory but no link
  132.             {
  133.                 while(ExNext((BPTR)dirlock,&InfoBlock))
  134.                 {
  135.                     strmfp(newpath,path,InfoBlock.fib_FileName);
  136.                     if(InfoBlock.fib_DirEntryType<0)                            // it's a file
  137.                     {
  138.                         if(mode==MODE_COMPLETE)                                        // if it matches the pattern then add to list
  139.                         {
  140.                             if(MatchPatternNoCase(pat,InfoBlock.fib_FileName)) { fprintf(wf,"%s\n",newpath);nr++; }
  141.                         }
  142.                         if(mode==MODE_INCREMENTAL)                                    // if it has changed & matches the pattern then add to list
  143.                         {
  144.                             if(!(InfoBlock.fib_Protection&FIBF_ARCHIVE))                // file has changed
  145.                             {
  146.                                 if(MatchPatternNoCase(pat,InfoBlock.fib_FileName)) { fprintf(wf,"%s\n",newpath);nr++; }
  147.                             }
  148.                         }
  149.                     }
  150.                     else nr=seekdir(wf,newpath,pat,mode,nr);                    // it's a directory
  151.                 }
  152.             }
  153.             else printf("ERROR: %s is no directory\n",path);
  154.         }
  155.         UnLock((BPTR)dirlock);
  156.     }
  157.     return(nr);
  158. }
  159.  
  160. int main(int argc,char *argv[])
  161. {
  162.     struct RDArgs *rda;
  163.     LONG args[ARG_COUNT]={0};
  164.     char dest[256],ndate[20],odate[10],path[256],tpat[30],pat[100],cmdln[256];
  165.     time_t t;
  166.     struct tm *p;
  167.     UBYTE nday,oday,nmon,omon;
  168.     FILE *tf,*wf;
  169.     UBYTE mode=MODE_NONE;            // default is - no backup
  170.     UBYTE force=FALSE;                // default is - we check the date
  171.     ULONG nr=0;
  172.     int numRetVal=RETURN_OK;
  173.  
  174.     REPORT_CALLS;
  175.  
  176.     BREAK(PRJ_NAME" has been started");
  177.  
  178.     if(rda=ReadArgs(ARG_TEMPLATE,args,NULL))
  179.     {
  180.         time(&t);p=localtime(&t);                    // get date & time
  181.         strftime(ndate,9,"%Y%m%d",p);ndate[9]=0;
  182.         strmfp(dest,(char *)args[ARGID_PATH],ndate);
  183.         if(args[ARGID_COMPLETE])     { force=TRUE;mode=MODE_COMPLETE; }        // force complete backup
  184.         if(args[ARGID_INCREMENTAL])    { force=TRUE;mode=MODE_INCREMENTAL; }    // force incremental backup
  185.         if(!force)                                                            // check date & time
  186.         {
  187.             if((wf=fopen("progdir:mbackup.last_backup","rb")))
  188.             {
  189.                 fread(odate,9,1,wf);odate[9]=0;
  190.                 fclose(wf);
  191.                 nmon=(ndate[4]-48)*10+(ndate[5]-48);
  192.                 omon=(odate[4]-48)*10+(odate[5]-48);
  193.                 if(nmon>omon) mode=MODE_COMPLETE;        // first backup in new month)
  194.                 else
  195.                 {
  196.                     nday=(ndate[6]-48)*10+(ndate[7]-48);
  197.                     oday=(odate[6]-48)*10+(odate[7]-48);
  198.                     if(nday>=oday+7) mode=MODE_INCREMENTAL;
  199.                 }
  200.             }
  201.             else mode=MODE_COMPLETE;
  202.         }
  203.         MSG1("force backup : %d",force);
  204.         MSG1("backup mode : %d",mode);
  205.         MSG1("backup date : %s",ndate);
  206.         if(mode)
  207.         {
  208.             if((wf=fopen("t:mbackup.tmp","wt")))
  209.             {
  210.                 if((tf=fopen((char *)args[ARGID_TASKFILE],"rt")))
  211.                 {
  212.                     while(!feof(tf))                    // build filelist
  213.                     {
  214.                         fscanf(tf,"%s %s",path,tpat);
  215.                         if(ParsePatternNoCase(tpat,pat,100)>=0)    nr=seekdir(wf,path,pat,mode,nr);
  216.                         else printf("ERROR: Wrong pattern %s\n",pat);
  217.                     }
  218.                     fclose(tf);
  219.                     fclose(wf);
  220.                     MSG1("%d files to backup",nr);
  221.                     if(nr>0)                            // if there are files to backup,
  222.                     {                                    // invoke lha
  223.                         if(mode==MODE_COMPLETE) sprintf(cmdln,"lha -it:mbackup.tmp -q -x -S a %sc\n",dest);
  224.                         if(mode==MODE_INCREMENTAL) sprintf(cmdln,"lha -it:mbackup.tmp -q -x -S a %si\n",dest);
  225.                         SystemTags(cmdln,SYS_Asynch,FALSE,SYS_Output,Output(),SYS_Input,Input());
  226.                     }
  227.                     DeleteFile("t:mbackup.tmp");
  228.                     MSG("lha finished");
  229.                     
  230.                     if(mode==MODE_COMPLETE) sprintf(cmdln,"%sc.lha",dest);
  231.                     if(mode==MODE_INCREMENTAL) sprintf(cmdln,"%si.lha",dest);
  232.                     if((tf=fopen(cmdln,"rt")))            // hat lha geklappt ?
  233.                     {
  234.                         fclose(tf);
  235.                         MSG("now saving status");
  236.                         if((wf=fopen("progdir:mbackup.last_backup","wb")))
  237.                         {
  238.                             time(&t);p=localtime(&t);                            // something is scrambling that field
  239.                             strftime(ndate,9,"%Y%m%d",p);ndate[9]=0;            // so I've to set this again
  240.                             fwrite(ndate,9,1,wf);
  241.                             fclose(wf);
  242.                         }
  243.                         if((wf=fopen("progdir:mbackup.log","at")))
  244.                         {
  245.                             strftime(ndate,20,"%d.%m.%Y",p);
  246.                             if(mode==MODE_COMPLETE) fprintf(wf,"Complete backup on %s\n",ndate);
  247.                             if(mode==MODE_INCREMENTAL) fprintf(wf,"Incremental backup on %s\n",ndate);
  248.                             fclose(wf);
  249.                         }
  250.                     }
  251.                 }
  252.                 else
  253.                 {
  254.                     fclose(wf);
  255.                     Error("open taskfile \"%s\"\n",args[ARGID_TASKFILE]);numRetVal=RETURN_ERROR;
  256.                 }
  257.             }
  258.             else { Error("open workfile \"t:mbackup.tmp\"\n");numRetVal=RETURN_ERROR; }
  259.         }
  260.         FreeArgs(rda);                                                            // free args
  261.     }
  262.     else { printf("Usage : "PRJ_NAME" %s\n",ARG_TEMPLATE);numRetVal=RETURN_WARN; }
  263.  
  264.     BREAK(PRJ_NAME" is ready");
  265.  
  266.     REPORT_OFF;
  267.  
  268.     return(numRetVal);
  269. }
  270.  
  271. //-- eof ----------------------------------------------------------------------
  272.